home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / UTILITIE / DOS_TEX.ZIP / TEX2ASC.C_ < prev    next >
Text File  |  1993-07-15  |  7KB  |  273 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5.  
  6. #define ML 512
  7. #define streq(A,B) (strcmp((A),(B))==0)
  8. #ifndef TRUE
  9. # define TRUE 1
  10. # define FALSE 0
  11. #endif
  12.  
  13. int indent;
  14. int wrap_index = 78;
  15.  
  16. void kilspaces(char *t)
  17. {
  18.      int i=0, j=0;
  19.  
  20.      strcat(t,"   ");
  21.  
  22.      while(t[i]!='\0')
  23.      {
  24.           while( t[i]!='\0' && strchr("\t\r\n ",t[i]) )
  25.                i++;
  26.           while( t[i]!='\0' && !strchr("\t\r\n ",t[i]) )
  27.           {
  28.                t[j]=t[i];
  29.                j++;
  30.                i++;
  31.           }
  32.           t[j++]=' ';
  33.      }
  34.      t[j]='\0';
  35.  
  36.      return;
  37. }
  38.  
  39. void force_out(char *t)
  40. {
  41.      int i, j;
  42.  
  43.      kilspaces(t);
  44.  
  45.      if(t[0]=='\0')
  46.      {
  47.           putchar('\n');
  48.           return;
  49.      }
  50.  
  51.      if(strlen(t)>wrap_index)
  52.      {
  53.           for(i=wrap_index-5*indent;t[i]!=' ';i--)
  54.                ;
  55.           t[i++]='\0';   /* i indexes the beginning of the next string */
  56.      }
  57.      else
  58.           i=0;
  59.  
  60.      if(indent)
  61.           fputs("     ",stdout);
  62.  
  63.      for(j=0;t[j]!='\0';j++)
  64.      {
  65.           if(t[j]=='~')
  66.                putchar(' ');
  67.           else if(t[j]=='.' && t[j+1]==' ')
  68.                fputs(". ",stdout);
  69.           else
  70.                putchar(t[j]);
  71.      }
  72.      putchar('\n');
  73.  
  74.      if(i)
  75.           strcpy(t,t+i);
  76.      else
  77.           t[0]='\0';
  78.  
  79.      indent=FALSE;
  80.  
  81.      return;
  82. }
  83.  
  84. void usage(void)
  85. {
  86.      puts("usage: tex2asc [-w wrap_limit]");
  87.      return;
  88. }
  89.  
  90. int main(int argc, char **argv)
  91. {
  92.      char c, cc, s[ML], t[ML];
  93.      int i, cr=0, t_index=0, chapter=1;
  94.      int control=FALSE, print_line=FALSE, center=FALSE;
  95.  
  96.      while(*++argv!=NULL)
  97.      {
  98.           if((*argv)[0]!='-')
  99.           {
  100.                usage();
  101.                return 1;
  102.           }
  103.           else
  104.                while(*(++*argv)!='\0')
  105.                     switch(**argv)
  106.                     {
  107.                          case 'w':
  108.                               if(*++argv!=NULL)
  109.                               {
  110.                                    wrap_index = atoi(*argv);
  111.                                    *argv+=(strlen(*argv)-1);
  112.                                    break;
  113.                               }
  114.                          default:
  115.                               usage();
  116.                               return 1;
  117.                     }
  118.      }
  119.  
  120.      indent=TRUE;
  121.      memset(t,'\0',ML-1);
  122.      while((c=getchar())!=EOF)
  123.      {
  124.           if(c=='%')             /* TeX comments */
  125.                while((c=getchar())!=EOF && c!='\n')
  126.                     ;
  127.           if(cr && c=='\n')
  128.           {
  129.                if(t_index)
  130.                {
  131.                     t[t_index]='\0';
  132.                     while(t[0]!='\0')
  133.                          force_out(t);
  134.                }
  135.                t_index=0;
  136.                indent=TRUE;   /* paragraph beginning */
  137.           }
  138.           if(control && isspace(c))
  139.                continue;
  140.           else
  141.                control=FALSE;
  142.           if(strchr("${}",c))      /* ignore these characters: ${} */
  143.                continue;
  144.           if(c=='\n')
  145.           {
  146.                cr++;
  147.                if(print_line)
  148.                {
  149.                     cr=0;
  150.                     t[t_index]='\0';
  151.                     kilspaces(t);
  152.                     if(center && wrap_index > strlen(t))
  153.                          for(i=0;i<(wrap_index-strlen(t))/2;i++)
  154.                               putchar(' ');
  155.  
  156.                     for(i=0;t[i]!='\0';i++)
  157.                          if(t[i]=='~')
  158.                               putchar(' ');
  159.                          else
  160.                               putchar(t[i]);
  161.                     putchar('\n');
  162.  
  163.                     t_index = 0;
  164.                     indent=FALSE;
  165.                     print_line = FALSE;
  166.                     center = FALSE;
  167.                     memset(t,'\0',ML);
  168.  
  169.                     continue;
  170.                }
  171.                else
  172.                     c=' ';
  173.           }
  174.           else
  175.                cr=0;
  176.  
  177.           if(c!='\\')
  178.                t[t_index++]=c;
  179.           else
  180.           {
  181.             control_code:
  182.                control=TRUE;
  183.                i=0;
  184.                while((c=getchar())!=EOF
  185.                     && !strchr("{}\\",c)
  186.                     && !isspace(c))
  187.                {
  188.                     s[i++]=c;
  189.                     if(!isalnum(c))
  190.                          break;
  191.                }
  192.                s[i]='\0';
  193.                cc=c;
  194.                if(streq(s,"-") || streq(s,"\""))
  195.                     ;         /* ignore */
  196.                else if(streq(s,"%"))
  197.                     t[t_index++]='%';
  198.                else if(streq(s,"bf"))
  199.                     ;
  200.                else if(streq(s,"bigskip"))
  201.                {
  202.                     t[t_index]='\0';
  203.                     while(t[0]!='\0')
  204.                          force_out(t);
  205.                     putchar('\n');
  206.                     t_index = 0;
  207.                }
  208.                else if(streq(s,"centerline"))
  209.                {
  210.                     center++;
  211.                     print_line++;
  212.                }
  213.                else if(streq(s,"chap"))
  214.                {
  215.                     sprintf(t+t_index," [%d] ",chapter++);
  216.                     if(chapter >= 100)
  217.                          t_index+= 2;
  218.                     else if(chapter >= 10)
  219.                          t_index += 1;
  220.                     t_index += strlen(" [1] ");
  221.                }
  222.                else if(streq(s,"dag"))
  223.                     t[t_index++]='%';
  224.                else if(streq(s,"end"))
  225.                     ;
  226.                else if(streq(s,"hbox"))
  227.                     ;
  228.                else if(streq(s,"input"))
  229.                     while((c=getchar())!='\n')
  230.                          ;
  231.                else if(streq(s,"langle"))
  232.                     t[t_index++]='<';
  233.                else if(streq(s,"line"))
  234.                     print_line++;
  235.                else if(streq(s,"linline"))
  236.                {
  237.                     print_line++;
  238.                     indent++;
  239.                }
  240.                else if(streq(s,"lline"))
  241.                     print_line++;
  242.                else if(streq(s,"narrower"))
  243.                     ;
  244.                else if(streq(s,"noindent"))
  245.                     indent=FALSE;
  246.                else if(streq(s,"poetrysize"))
  247.                     wrap_index=50;
  248.                else if(streq(s,"raggedbottom"))
  249.                     ;
  250.                else if(streq(s,"rangle"))
  251.                     t[t_index++]='>';
  252.                else if(streq(s,"sc"))
  253.                     ;
  254.                else if(streq(s,"sec"))
  255.                     ;
  256.                else if(streq(s,"vbox"))
  257.                     ;
  258.                else if(streq(s,"vfill"))
  259.                     ;
  260.                if((c=cc)=='\\')
  261.                     goto control_code;
  262.           }
  263.           if( !print_line && isspace(t[t_index-1]) && t_index >
  264.                                                   wrap_index+20 )
  265.           {
  266.                t[t_index]='\0';
  267.                force_out(t);
  268.                t_index = strlen(t);
  269.           }
  270.      }
  271.      return 0;
  272. }
  273.